﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;

namespace Portal.CMS
{
    public class NewsUploadDocument : WebPart
    {

        public override string WebPartId
        {
            get
            {
                return "NewsUploadDocument";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Upload tài liệu cho bản tin");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }


        System.Web.UI.WebControls.Button SaveButtonLargeOnly = null;
        System.Web.UI.WebControls.Label labelError = null;
        System.Web.UI.WebControls.TextBox txtNewsItemName = null;
        System.Web.UI.WebControls.TextBox txtSubject = null;
        System.Web.UI.WebControls.TextBox txtFriendlyTitle = null;
        System.Web.UI.WebControls.TextBox txtSortIndex = null;

        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl, System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(NewsUploadDocument));

            labelError = new System.Web.UI.WebControls.Label();
            labelError.CssClass = "ErrorNewsUploadDocumentImage";

            SaveButtonLargeOnly = new Button();
            SaveButtonLargeOnly.Text = ResourceBLL.GetText(WebPartId, "Upload", "Upload tài liệu");
            SaveButtonLargeOnly.Click += new EventHandler(SaveButtonLargeOnly_Click);
           
            txtNewsItemName = new TextBox();
            txtNewsItemName.CssClass = "cssUploadAdvImageItem";

            txtSubject = new TextBox();
            txtSubject.CssClass = "cssUploadAdvImageItem";

            txtFriendlyTitle = new TextBox();
            txtFriendlyTitle.CssClass = "cssUploadAdvImageItem";

            txtSortIndex = new TextBox();
            txtSortIndex.CssClass = "cssUploadAdvImageItem";

            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetHeaderFormDialog(Title)));
            pl.Controls.Add(labelError);
            pl.Controls.Add(new System.Web.UI.LiteralControl("<table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText(WebPartId, "FileName", "Upload file:")));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<input name=\"txtItemName\" id=\"txtItemName\" type=\"file\" runat=server>"));
            pl.Controls.Add(SaveButtonLargeOnly);
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));


            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>" + ResourceBLL.GetText(WebPartId, "Subject", "Tiêu đề") + ":</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(txtSubject);
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));



            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>" + ResourceBLL.GetText(WebPartId, "FriendlyTitle", "Tên hiển thị") + ":</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(txtFriendlyTitle);
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));


            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>" + ResourceBLL.GetText(WebPartId, "SortIndex", "Thứ tự hiển thị") + ":</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(txtSortIndex);
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));

            
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td></td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));

            pl.Controls.Add(new System.Web.UI.LiteralControl("<input type=button onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "CloseButton", "Thoát") + "\">"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetFooterFormDialog()));
        }

        void SaveButtonLargeOnly_Click(object sender, EventArgs e)
        {
            try
            {
                string NewsId = (string)System.Web.HttpContext.Current.Request["NewsId"];
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
                labelError.Text = "";
                
                string Subject = txtSubject.Text;

                string FriendlyTitle = txtFriendlyTitle.Text;
                string SortIndex = txtSortIndex.Text;

                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

                HttpPostedFile hf = (HttpPostedFile)files[0];
                string WorkingPath = Environments.WorkingPath;
                string PathSave = WorkingPath + "\\media\\" + Environments.PortalAlias;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\media\\" + Environments.PortalAlias + "\\News";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\media\\" + Environments.PortalAlias + "\\News\\Archives";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\media\\" + Environments.PortalAlias + "\\News\\Archives\\" + NewsCls.belongSite;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                string FileSave = PathSave + "\\" + hf.FileName;
                string UploadName = "";
                if (hf.FileName.Length > 0)
                {
                    try
                    {
                        hf.SaveAs(FileSave);
                        byte[] b = Environments.GetBytesFromFile(FileSave);
                        Environments.UploadFileWithWs(FileSave, b, WorkingPath);

                        UploadName = new System.IO.FileInfo(FileSave).Name;
                    }
                    catch { UploadName = ""; }
                }
                else
                {
                    FileSave = "";
                }
                if (string.IsNullOrEmpty(SortIndex)) {
                    SortIndex = "0";
                }
                ServerSideUploadDocument(NewsCls.belongSite, NewsId, FileSave, Subject, FriendlyTitle, int.Parse(SortIndex));
                labelError.Text = ResourceBLL.GetText(WebPartId, "UploadOk", "Thêm mới tài liệu vào thành công!");
                txtNewsItemName.Text = "";
            }
            catch (Exception ex)
            {
                labelError.Text = ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideUploadDocument(string belongSite, string NewsId, string UploadFile, string Subject,string  FriendlyTitle, int SortIndex)
        {
            new NewsBLL().UploadMultiDocument(null, belongSite, NewsId, UploadFile,Subject,FriendlyTitle,SortIndex);
            return ResourceBLL.GetText(WebPartId, "MessageUploadOk", "Thêm mới và upload thành công!");
        }
    }
}